<?php
// initial session
session_start();
require '../model/user.php';
$user = new User();

// xu ly bien
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

//
$msg = array();
$status = "";
$role = "";

// if empty password or username

if ($user->checkLoginUser($username, $password) == true)
{
    $msg['warning'] = "Login successful";
    $status = "OK";
    // display role of user
    $userRole = $user->getUserRole($username);

    switch ($userRole) {
        case 'admin':
        {
            $role = 'admin';
            $_SESSION['username'] = $username;
        }
        break;
        case 'user':
        {
            $accInfo = $user->getUserInfo($username);
            $role = 'user';
            $_SESSION['username'] = $username;
            $_SESSION['accInfo'] = $accInfo;
        }
        break;
    }
} else { // kiem tra dang nhap khong thanh cong
    $msg['warning'] = "Sai mật khẩu hoặc ussername không tồn tại";
    $status = "error";
}

$response = array("status" => $status, "message" => $msg, "role" => $role);
$jsonString = json_encode($response);

echo $jsonString;
